package org.wisdom.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.wisdom.client.MTClient;
import org.wisdom.mapper.UserProfileMapper;
import org.wisdom.request.PutPersonalDetailsRequest;
import org.wisdom.response.PersonalDetailsResponse;
import org.wisdom.service.UserProfileService;
import org.wisdom.utils.UserContext;

import java.io.IOException;

@Service
public class UserProfileServiceImpl implements UserProfileService {

    @Autowired
    UserProfileMapper userProfileMapper;

    @Autowired
    MTClient mtClient;

    @Override
    public String getAvatar() {
       return userProfileMapper.selectAvatarById(UserContext.getUser());
    }

    @Override
    public void postAvatar(MultipartFile file) throws IOException {
        String url = mtClient.upload(file);
        userProfileMapper.updateAvatarById(UserContext.getUser(), url);
    }

    @Override
    public void putPersonalDetails(PutPersonalDetailsRequest putPersonalDetailsRequest) {
        userProfileMapper.updatePersonalDetailsById(UserContext.getUser(), putPersonalDetailsRequest);
    }

    @Override
    public PersonalDetailsResponse getPersonalDetails() {
        return userProfileMapper.selectPersonalDetailsById(UserContext.getUser());
    }

}
